// 前端 roles 控制菜单权限 通过登录后的角色对菜单就行过滤处理
// 如果需要前端 roles 控制菜单权限 请使用此文件代码替换 permission.ts 的内容
import { RouteRecordRaw } from 'vue-router';

import router, { asyncRouterList } from '@/router';
import { store } from '@/store';

export const usePermissionStore = defineStore('permission', {
  state: () => ({
    routers: [] as RouteRecordRaw[],
  }),
  actions: {
    async initRoutes() {
      let accessedRouters = [];
      accessedRouters = asyncRouterList;
      this.routers = accessedRouters;
      accessedRouters.forEach((route) => {
        router.addRoute(route);
      });
    },
    async restoreRoutes() {
      // 不需要在此额外调用initRoutes更新侧边导肮内容，在登录后asyncRoutes为空会调用
      this.routers.forEach((item) => {
        if (item.name) {
          router.removeRoute(item.name as string);
        }
      });
      this.routers = [];
    },
  },
});

export function getPermissionStore() {
  return usePermissionStore(store);
}
